﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BISPOS.Repository;
using System.Linq;
namespace BISPOS
{
    public partial class RefundProductsForm : DevExpress.XtraEditors.XtraForm
    {

        ProductRepository productRepository = new ProductRepository();
        InvoiceRepository invoiceRepository = new InvoiceRepository();

        List<InvoiceItem> itemToRemove = new List<InvoiceItem>();
        List<InvoiceItem> allInvoiceItem = new List<InvoiceItem>();
        Invoice invoice;
        public RefundProductsForm(List<InvoiceItem> _itemToRemove,Invoice _invoice)
        {
            InitializeComponent();
            itemToRemove = _itemToRemove;
            invoice = _invoice;
            allInvoiceItem = invoice.InvoiceItems.ToList();
            foreach (var item in itemToRemove)
            {
                allInvoiceItem.Remove(item);
            }
            invoiceItemsLinqServerModeSource.QueryableSource = allInvoiceItem.AsQueryable();

            oldInvoiceIdTextEdit.Text = invoice.Id.ToString().PadLeft(8, '0');
            oldInvoiceDateTextEdit.Text = invoice.Date.ToShortDateString() + ", " + invoice.Date.ToLongTimeString();
            try
            {
                oldCustomerTextEdit.Text = invoice.Customer.Name;
            }
            catch { oldCustomerTextEdit.Text = "ทั่วไป"; }

            refundPriceTextEdit.Text = (itemToRemove.Sum(x => x.TotalPrice)).ToString("n2");
            newTotalPriceTextEdit.Text = (allInvoiceItem.Sum(x => x.TotalPrice)).ToString("n2");
            oldTotalPriceTextEdit.Text = (itemToRemove.Sum(x => x.TotalPrice) + allInvoiceItem.Sum(x => x.TotalPrice)).ToString("n2");

            if (allInvoiceItem.Count == 0)
            {
                newInvoiceLayoutControlGroup.Text = "ไม่มีการออกใบเสร็จใหม่";
                newInvoiceLayoutControlGroup.Enabled = false;
              
            }
        }
        private void RefundProductsForm_Load(object sender, EventArgs e)
        {
            
        }
        private void RefundProductsForm_Shown(object sender, EventArgs e)
        {
        }

        private void calcellButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void oKButton_Click(object sender, EventArgs e)
        {
            BISPOSDataClassesDataContext db = new BISPOSDataClassesDataContext();
            this.invoice.IsActive = false;
            db.SubmitChanges();
            this.Close();
           
        }
        private void Refund()
        { 
          /*  invoice.IsActive = false;
            
            string productsNametoRemove = "";
            foreach(var remove in itemToRemove)
            {
                productsNametoRemove += remove.Stock.Product.BarcodeAndName + " " + remove.Quantity.ToString("n2") + " " + remove.Stock.Product.Unit + ", ";
            }
            productsNametoRemove.Remove(productsNametoRemove.Length - 1);

            Bill newBill = new Bill
            {
                IsCredit = false,
                Note = "รัยคืนสินค้า : " + productsNametoRemove,
                SupplierId = 
            };*/
        }
        
    }
}